package dao;

import entity.Student;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import utils.DruidUtils;

import java.sql.SQLException;
import java.util.List;

public class StudentDaoImp implements StudentDao{

    //获取学生列表
    @Override
    public List<Student> getStudentList(int pageindex, int perpage) {
        try {
            QueryRunner queryRunner = new QueryRunner(DruidUtils.getDataSource());

            String sql = "select * from student limit ?,?";
            Object[] param = {pageindex,perpage};

            List<Student> students = queryRunner.query(sql, new BeanListHandler<Student>(Student.class), param);
            return students;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    //获取学生个数
    @Override
    public long getStudentsCount() {
        try {
            QueryRunner queryRunner = new QueryRunner(DruidUtils.getDataSource());

            String sql = "select count(1) from student";

            long count = queryRunner.query(sql, new ScalarHandler<>());

            return count;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    //添加学生
    @Override
    public void insertSTudent(Student student) {
        try {
            QueryRunner queryRunner =new QueryRunner(DruidUtils.getDataSource());

            String sql = "insert into student values(null,?,?,?,?,?,?)";

            Object[] param = {student.getSid(),student.getSname(),student.getSex(),student.getBirthday(),student.getEmail(),student.getRemarks()};

            queryRunner.update(sql,param);
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }

    //删除学生
    @Override
    public void deleteStudent(int id) {
        try {
            QueryRunner queryRunner = new QueryRunner(DruidUtils.getDataSource());

            String sql = "delete from student where id =?";
            queryRunner.update(sql,id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    //修改学生
    @Override
    public void changeStudent(Student student) {
        try {
            QueryRunner queryRunner = new QueryRunner(DruidUtils.getDataSource());

            String sql = "update student set sid=?,sname=?,sex=?,birthday=?,email=?,remarks=? where id = ?";

            Object[] param = {student.getSid(),student.getSname(),student.getSex(),student.getBirthday(),student.getEmail(),student.getRemarks(),student.getId()};

            queryRunner.update(sql,param);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    //根据学号查找学生
    @Override
    public List<Student> findStudentBySid(String sid) {
        try {
            QueryRunner queryRunner = new QueryRunner(DruidUtils.getDataSource());

            String sql = "select * from student where sid = ?";

            List<Student> students = queryRunner.query(sql, new BeanListHandler<Student>(Student.class), sid);
            return students;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    //该学号学生是否已经存在
    @Override
    public boolean ishave(String sid) {
        Student student = null;
        try {
            QueryRunner queryRunner = new QueryRunner(DruidUtils.getDataSource());

            String sql = "select * from student where sid = ?";

            student = queryRunner.query(sql, new BeanHandler<Student>(Student.class), sid);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        if (student == null){
            return false;
        }else {
            return true;
        }

    }

}
